using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AjGenesisStudio.Properties;

namespace AjGenesisStudio.Dialogs
{
    public partial class NewProjectDialog : Form
    {
        public NewProjectDialog()
        {
            InitializeComponent();
        }

        private void NewProjectDialog_Load(object sender, EventArgs e)
        {
            FillList();
            FillDirectories();
        }

        private void FillList()
        {
            string dir_path;
            ListViewItem item;

            dir_path = AppDomain.CurrentDomain.BaseDirectory + "ProjectTemplates";
            if (!Directory.Exists(dir_path))
            {
                MessageBox.Show("Can not find the 'ProjectTemplates' directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            foreach (string folder in Directory.GetDirectories(dir_path))
            {
                item = new ListViewItem();
                item.Text = (new DirectoryInfo(folder)).Name;
                item.Tag = folder;
                item.ImageIndex = 0;
                lstProjects.Items.Add(item);
            }
        }

        private void FillDirectories()
        {
            cboDirectories.Items.Clear();
            if (Settings.Default.NewProjectDirs != null)
            {
                foreach (string i in Settings.Default.NewProjectDirs)
                    cboDirectories.Items.Add(i);
                cboDirectories.Text = cboDirectories.Items[0].ToString();
            }
            else
            {
                cboDirectories.Text = AppDomain.CurrentDomain.BaseDirectory;
            }
        }

        private void btnExplore_Click(object sender, EventArgs e)
        {
            if (dlg.ShowDialog() == DialogResult.OK)
                cboDirectories.Text = dlg.SelectedPath;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string path;
            Commands.OpenProjectCommand cmd;

            if (string.IsNullOrEmpty(cboDirectories.Text))
            {
                cboDirectories.Select();
                return;
            }
            if (string.IsNullOrEmpty(txtProjectName.Text))
            {
                txtProjectName.Select();
                return;
            }
            if (Directory.Exists(txtProjectName.Text))
            {
                MessageBox.Show("The destination directory do not exist");
                cboDirectories.Select();
                return;
            }
            SaveDirectoriesSettings();

            path = Path.Combine(cboDirectories.Text, txtProjectName.Text);

            copyDirectory(lstProjects.SelectedItems[0].Tag.ToString(), path);
            
            cmd = new Commands.OpenProjectCommand();
            cmd.Execute(path);
            
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void SaveDirectoriesSettings()
        {
            if (Settings.Default.NewProjectDirs == null)
                Settings.Default.NewProjectDirs = new System.Collections.Specialized.StringCollection();

            if (!Settings.Default.NewProjectDirs.Contains(cboDirectories.Text))
                Settings.Default.NewProjectDirs.Insert(0, cboDirectories.Text);
        }

        public static void copyDirectory(string Src,string Dst){
            String[] Files;
            
            if(Dst[Dst.Length-1] != Path.DirectorySeparatorChar) 
                Dst += Path.DirectorySeparatorChar;

            if(!Directory.Exists(Dst))
                Directory.CreateDirectory(Dst);
            
            Files = Directory.GetFileSystemEntries(Src);
            
            foreach(string Element in Files)
            {
                if(Directory.Exists(Element)) 
                    copyDirectory(Element, Dst + Path.GetFileName(Element));
                else 
                    File.Copy(Element, Dst + Path.GetFileName(Element), true);
            }
        }

        private void lstProjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = (lstProjects.SelectedItems.Count > 0);
        }

        private void btnLarge_CheckedChanged(object sender, EventArgs e)
        {
            if (btnLarge.Checked)
                lstProjects.View = View.LargeIcon;
            else
                lstProjects.View = View.SmallIcon;
        }

        private void btnSmall_CheckedChanged(object sender, EventArgs e)
        {
            if (btnSmall.Checked)
                lstProjects.View = View.SmallIcon;
            else
                lstProjects.View = View.LargeIcon;
        }
    }
}